<?php

namespace app\common\model;

use app\common\model\Common as CommonModel;
/**
 * 后台用户管理model类
 * @package Home\Model
 */
class Admin extends CommonModel
{
    protected $pk = 'admin_id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 20;
        }
        $field = 'a.*';
        $field .= ',ar.role_name';

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->alias("a")
            ->join('admin_role ar','a.role_id=ar.role_id','left')
            ->field($field)
            ->where('a.admin_id','>',1)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);


        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];

        if(isset($post['username']) && !empty($post['username'])){
            $where[] = ['username','like','%'.$post['username'].'%'];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = [$pk => 'desc'];
        return $result;
    }

    protected function tableFormat($list)
    {

        foreach ($list as $k=>$v){
            $list[$k]['add_time'] = date('Y-m-d H:i',$v['add_time']);
        }
        return $list;
    }

    public function encrypt($str,$ec_salt){
        return md5($str.$ec_salt);
    }


    public function handle($param){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];

        if($param['password'] != $param['repassword']){
            $result['msg'] = '两次密码不一致';
            return $result;
        }

        $act = $param['act'];

        $data['mobile'] = isset($param['mobile'])?$param['mobile']:'';
        $data['email'] = isset($param['email'])?$param['email']:'';
        $data['role_id'] = $param['role_id'];

        if($act == 'add'){
            $data['username'] = $param['username'];
            $data['add_time'] = time();
            $rand = md5(time());
            $str = substr($rand,0,6);
            $data['ec_salt'] = $str;
            if(!$param['password']){
                $data['password'] = $this->encrypt($param['password'],$data['ec_salt']) ;
            }else{
                $data['password'] = md5('123456'.$data['ec_salt']);
            }
            $r = $this->insertGetId($data);
            $id = $r;
            $ty = '添加';
        }

        if($act == 'edit'){
            $data['update_time'] = time();
            if(!empty($param['password'])){
                $rand = md5(time());
                $str = substr($rand,0,6);
                $data['ec_salt'] = $str;
                $data['password'] = $this->encrypt($param['password'],$data['ec_salt']) ;
            }
            $r = $this->where('admin_id','=',$param['admin_id'])->update($data);
            $id = $param['admin_id'];
            $ty = '修改';
        }

        if(!$r){
            $result['msg'] = '操作失败';
            return $result;
        }
        adminLog("后台管理员".$ty,$id);
        $result['code'] = 1;
        $result['msg'] = '操作成功';
        return $result;
    }


}